import axios, { AxiosResponse, AxiosError } from 'axios';
import { RespData } from '../entity/response/RespData';
// import mpAdapter from 'axios-miniprogram-adapter'
// import AxiosRequestConfig from "axios-miniprogram-adapter";
// axios.defaults.adapter = mpAdapter;

const axiosInstance = axios.create({
  timeout: 10000,
});

axiosInstance.interceptors.response.use(
    (response: AxiosResponse) => {
    const respData = new RespData(response.data);
    if (respData.getCode() !== 200) {
      return Promise.reject(new Error(respData.getMessage()));
    }
    return response;
  },
  (error: AxiosError) => {
    return Promise.reject(error);
  }
);

export const setHost = (host: string) => {
  axiosInstance.defaults.baseURL = host;
};

export default axiosInstance;
